<?php
declare (strict_types = 1);
/**
 * 素材模型
 */
namespace app\common\model;

class AdvPgmList extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_adv_pgmlist';

    public function matInfoHasOne()
    {
        return $this->hasOne(Material::class, 'mat_id', 'mat_id');
    }

    public function detailLists($where = [], $ordername='play_date', $order= 'asc') {

        $whereMat = [];
        $field = '*';

        $list = self::with([
            'matInfoHasOne' => function ($query) use ($whereMat) {
                $query->withField(['mat_id','mat_name', 'file_name', 'file_len', 'markin', 'markout'])->hidden(['mat_id'])->where($whereMat);
            }
        ])->field($field)->where($where)->order([$ordername => $order])->select()->toArray();

        foreach($list as $k =>$v) {
            if($v['matInfoHasOne'] != null) {
                $list[$k] = array_merge($v['matInfoHasOne'], $list[$k]);                               
            }
            unset($list[$k]['matInfoHasOne']);
        }

        return $list;
    }

    public function adddatas($param, $where)
    {
        if(isset($param['up_time'])) {
            $param['up_time']  = date('Y-m-d H:i:s',time());
        }
        if(isset($param['create_time'])) {
            $param['create_time'] = date('Y-m-d H:i:s',time());
        }        
        
        $save = self::where($where)->insertGetId($param);

        if ($save === false) exception('添加失败');

        return $save;
    }
}
